package one;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import myjavab.DBUtil;
import myjavab.Student;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class MyJFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	DBUtil db=new DBUtil();
	ArrayList<Student> arlist=new ArrayList<Student>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyJFrame frame = new MyJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 623, 496);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmConnect = new JMenuItem("Connect");
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				db.getConnection();
			}
		});
		mnFile.add(mntmConnect);
		
		JMenuItem mntmDisconnect = new JMenuItem("DisConnect");
		mnFile.add(mntmDisconnect);
		contentPane = new JPanel();
		contentPane.setVisible(true);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPleaseEnterThe = new JLabel("please enter the student id:");
		lblPleaseEnterThe.setBounds(21, 10, 179, 15);
		contentPane.add(lblPleaseEnterThe);
		
		
		textField = new JTextField();
		textField.setBounds(221, 7, 161, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(25, 52, 492, 374);
		contentPane.add(textArea);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setVisible(true);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input=textField.getText().trim();
				if(input.length()==0) {
					arlist.clear();
					arlist=db.executeQueryAll();
					for(Student ee:arlist) {
						textArea.append(ee.getSid()+" "+ee.getSname()+" "+ee.getSdept()+" "+ee.getAge()+" "+ee.getScore()+"\n");
							
					}
				}else {
					arlist.clear();
					textArea.setText("");
					arlist=db.executeQueryByld(input);
					for(Student ee:arlist) {
						textArea.append(ee.getSid()+" "+ee.getSname()+" "+ee.getSdept()+" "+ee.getAge()+" "+ee.getScore()+"\n");
							
					}
				}
				
			}
		});
		btnSearch.setBounds(424, 6, 93, 23);
		contentPane.add(btnSearch);
		

	}
}
